package com.example.algorithm.no0686.solution;

import com.example.algorithm.no0686.Solution;

/**
 * @Description 解答- 重复叠加字符串匹配
 * @ClassName SolutionImpl
 * @Author Administrator
 * @Date 2020/5/20 10:26
 * @Version 1.0.0
 */
public class SolutionImpl implements Solution {
    @Override
    public int repeatedStringMatch(String A, String B) {
        // 最大长度为B字符串两边都叠加A串
        int end = B.length() + A.length() * 2;
        StringBuilder sb = new StringBuilder(A);
        while (sb.length() < end && sb.lastIndexOf(B) == -1) {
            sb.append(A);
        }
        return sb.lastIndexOf(B) != -1 ? sb.length() / A.length() : -1;
    }
}
